using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using PmpNetwork.Extensions;
using PmpNetwork.Models;
using PmpNetwork.Services;

namespace PmpNetwork {
    public class Migrations : DataMigrationImpl
    {
        private readonly ITaxonomyImportService _taxonomyImportService;

        public Migrations(ITaxonomyImportService taxonomyImportService)
        {
            _taxonomyImportService = taxonomyImportService;
        }
        public int Create()
        {
            SchemaBuilder.CreateTable("WikiArticlePartRecord", table => table
                    .ContentPartRecord()
                    .Column<int>("AuthorId")
                    .Column<string>("Name", c => c.WithLength(255))
                    .Column<string>("NameOnUrl", c => c.WithLength(255))
                    .Column<string>("Content", c => c.Unlimited())
                    .Column<DateTime>("CreatedDate", c => c.Nullable())
                    .Column<DateTime>("LastUpdatedDate", c => c.Nullable())
                    .Column<int>("TotalViews")
                );

            SchemaBuilder.CreateTable("WikiQuestionPartRecord", table => table
                    .ContentPartRecord()
                    .Column<int>("ArticleId")
                    .Column<string>("Text", c => c.WithLength(4000))
                    .Column<int>("DisplayIndex", c => c.WithDefault(0))
                    .Column<string>("Reason", c => c.WithLength(4000))
                );

            SchemaBuilder.CreateTable("WikiQuestionOptionPartRecord", table => table
                    .ContentPartRecord()
                    .Column<int>("QuestionId")
                    .Column<string>("Text", c => c.WithLength(255))
                    .Column<int>("DisplayIndex")
                    .Column<bool>("IsCorrect", c => c.Nullable())
                );

            ContentDefinitionManager.AlterTypeDefinition("WikiArticle", t => t
                .WithPart(typeof(WikiArticlePart).Name)
                );

            ContentDefinitionManager.AlterTypeDefinition("WikiQuestion", t => t
                .WithPart(typeof(WikiQuestionPart).Name)
                );

            ContentDefinitionManager.AlterTypeDefinition("WikiQuestionOption", t => t
                .WithPart(typeof(WikiQuestionOptionPart).Name)
                );

            ContentDefinitionManager.AlterPartDefinition("WikiArticlePart",
                    builder => builder.Attachable(false));

            ContentDefinitionManager.AlterPartDefinition("WikiQuestionPart",
                    builder => builder.Attachable(false));

            ContentDefinitionManager.AlterPartDefinition("WikiQuestionOptionPart",
                    builder => builder.Attachable(false));


            var categoryTaxonomyPart = _taxonomyImportService.CreateTaxonomy(Constants.Taxonomies.Categories);
            ContentDefinitionManager.AlterPartDefinition("WikiArticlePart", builder => builder
                .WithField(Constants.Taxonomies.Categories, cfg => cfg
                    .OfType("TaxonomyField")
                    .WithSetting("TaxonomyFieldSettings.AllowCustomTerms", "true")
                    .WithSetting("TaxonomyFieldSettings.SingleChoice", "true")
                    .WithSetting("TaxonomyFieldSettings.Required", "true")
                    .WithSetting("TaxonomyFieldSettings.Taxonomy", categoryTaxonomyPart.Name)));

            return 5;
        }

        public int UpdateFrom1()
        {
            SchemaBuilder.AlterTable("WikiQuestionOptionPartRecord", table => table
                .AddColumn<bool>("IsCorrect", c => c.Nullable()));
            return 2;
        }

        public int UpdateFrom2()
        {
            SchemaBuilder.AlterTable("WikiQuestionPartRecord", table => table
                .AddColumn<int>("DisplayIndex", c => c.WithDefault(0)));
            return 3;
        }

        public int UpdateFrom3()
        {
            SchemaBuilder.AlterTable("WikiQuestionPartRecord", table => table.DropColumn("CorrectOptionId"));
            SchemaBuilder.AlterTable("WikiQuestionPartRecord", table => table.AddColumn<string>("Reason", c => c.WithLength(4000)));
            return 4;
        }

        public int UpdateFrom4()
        {
            SchemaBuilder.DropForeignKey("WikiQuestionOptionPartRecord", "Option_Question");
            SchemaBuilder.DropForeignKey("WikiQuestionPartRecord", "Question_WikiArticle");
            return 5;
        }
        public int UpdateFrom5()
        {
            SchemaBuilder.CreateTable("CategoryTreePartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<int>("ContentMode")
                    .Column<string>("WidthCss", c => c.WithLength(255))
                    .Column<string>("HeightCss", c => c.WithLength(255))
                    .Column<string>("OverflowCss", c => c.WithLength(255))
                    .Column<int>("MaxLevel")
                );

            ContentDefinitionManager.AlterTypeDefinition("CategoryTree",
                cfg => cfg
                    .WithPart("CategoryTreePart")
                    .WithPart("CommonPart")
                    .WithPart("WidgetPart")
                    .WithSetting("Stereotype", "Widget")
                );
            return 6;
        }
    }
}